using System.Net;
using System.Runtime.CompilerServices;
using System.Threading;
using LiteNetLib.Utils;

namespace LiteNetLib
{
	public class ConnectionRequest
	{
		private readonly IConnectionRequestListener _listener;

		private int _used;

		public readonly NetDataReader Data;

		[CompilerGenerated]
		private ConnectionRequestResult _003CResult_003Ek__BackingField;

		[CompilerGenerated]
		private ConnectionRequestType _003CType_003Ek__BackingField;

		internal readonly long ConnectionId;

		internal readonly byte ConnectionNumber;

		internal readonly NetPeer Peer;

		public IPEndPoint RemoteEndPoint
		{
			get
			{
				return Peer.EndPoint;
			}
		}

		public ConnectionRequestResult Result
		{
			[CompilerGenerated]
			get
			{
				return _003CResult_003Ek__BackingField;
			}
			[CompilerGenerated]
			private set
			{
				_003CResult_003Ek__BackingField = value;
			}
		}

		public ConnectionRequestType Type
		{
			[CompilerGenerated]
			get
			{
				return _003CType_003Ek__BackingField;
			}
			[CompilerGenerated]
			private set
			{
				_003CType_003Ek__BackingField = value;
			}
		}

		private bool TryActivate()
		{
			return Interlocked.CompareExchange(ref _used, 1, 0) == 0;
		}

		internal ConnectionRequest(long connectionId, byte connectionNumber, ConnectionRequestType type, NetDataReader netDataReader, NetPeer peer, IConnectionRequestListener listener)
		{
			ConnectionId = connectionId;
			ConnectionNumber = connectionNumber;
			Type = type;
			Peer = peer;
			Data = netDataReader;
			_listener = listener;
		}

		public NetPeer AcceptIfKey(string key)
		{
			if (!TryActivate())
			{
				return null;
			}
			try
			{
				if (Data.GetString() == key)
				{
					Result = ConnectionRequestResult.Accept;
					_listener.OnConnectionSolved(this, null, 0, 0);
					return Peer;
				}
			}
			catch
			{
				NetDebug.WriteError("[AC] Invalid incoming data");
			}
			Result = ConnectionRequestResult.Reject;
			_listener.OnConnectionSolved(this, null, 0, 0);
			return null;
		}

		public NetPeer Accept()
		{
			if (!TryActivate())
			{
				return null;
			}
			Result = ConnectionRequestResult.Accept;
			_listener.OnConnectionSolved(this, null, 0, 0);
			return Peer;
		}

		public void Reject(byte[] rejectData, int start, int length)
		{
			if (TryActivate())
			{
				Result = ConnectionRequestResult.Reject;
				_listener.OnConnectionSolved(this, rejectData, start, length);
			}
		}

		public void Reject()
		{
			Reject(null, 0, 0);
		}

		public void Reject(byte[] rejectData)
		{
			Reject(rejectData, 0, rejectData.Length);
		}

		public void Reject(NetDataWriter rejectData)
		{
			Reject(rejectData.Data, 0, rejectData.Length);
		}
	}
}
